home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / GLX / rubberband / rubberbandXlib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.7 KB  |  215 lines

  1. /*
  2.  * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <X11/Xlib.h>
  18. #include <X11/Xutil.h>
  19. #include <gl/gl.h>
  20. #include <gl/glws.h>
  21.  
  22. GLXconfig conf[] = {
  23.     {GLX_NORMAL,  GLX_DOUBLE, False},
  24.     {GLX_NORMAL,  GLX_RGB,    True},
  25.     {0, 0, 0}
  26. };
  27.  
  28. main(argc, argv)
  29.  
  30. int argc;
  31. char *argv[];
  32.  
  33. {
  34.  
  35.     float ptv[2];
  36.     int i;
  37.     int screen;
  38.     int flags;
  39.  
  40.     XVisualInfo *GetGLXVisual();
  41.     void SetGLXWindow();
  42.     unsigned long GetGLXConfigValue();
  43.     Display *dpy;
  44.     Window gr_window;
  45.     XVisualInfo *gv;
  46.     XSetWindowAttributes attributes;
  47.     GLXconfig *result_conf;
  48.     XEvent event;
  49.  
  50.     if (!(dpy = XOpenDisplay("")) ) {
  51.     perror("Cannot open display\n");
  52.     exit(-1);
  53.     }
  54.  
  55.     screen = DefaultScreen(dpy);
  56.     XSynchronize(dpy,True);
  57.  
  58.     if ((result_conf = GLXgetconfig(dpy, screen, conf)) == 0) {
  59.     printf("GLXgetconfig failed \n");
  60.     exit(-1);
  61.     }
  62.  
  63.     /* get the visual for the normal planes */
  64.     gv = GetGLXVisual (dpy, GLX_NORMAL, result_conf);
  65.     printf("Normal buffer id = 0x%x, depth = %d\n", gv->visualid, gv->depth);
  66.  
  67.     /* Set up an GLX window in the normal image planes.    */ 
  68.  
  69.     attributes.border_pixel = 0;
  70.     attributes.background_pixel = 0x0;
  71.     attributes.bit_gravity  = ForgetGravity;
  72.     flags = CWBackPixel | CWBorderPixel | CWBitGravity;
  73.     flags |= CWColormap;
  74.     attributes.colormap = GetGLXConfigValue (GLX_NORMAL, GLX_COLORMAP,
  75.                              result_conf);
  76.     printf("Normal buffer cmap  = 0x%x\n", attributes.colormap);
  77.  
  78.  
  79.     gr_window = XCreateWindow(dpy,
  80.                            RootWindow(dpy,screen),
  81.                            100, 
  82.                100, 
  83.                            256, 
  84.                            256, 
  85.                            4, 
  86.                            gv->depth, 
  87.                            InputOutput,
  88.                            gv->visual,
  89.                flags,
  90.                &attributes);
  91.  
  92.     /* Select input and map the windows.    */
  93.     XSelectInput (dpy, gr_window, ExposureMask | StructureNotifyMask |
  94.             ResizeRedirectMask | PointerMotionMask);
  95.  
  96.     XStoreName(dpy,gr_window, "Rubber Banding");
  97.  
  98.     XMapRaised(dpy, gr_window);
  99.     XWindowEvent (dpy, gr_window, ExposureMask, &event);
  100.     while (XCheckWindowEvent(dpy, gr_window, ExposureMask | StructureNotifyMask,
  101.                 &event));
  102.  
  103.  
  104.     SetGLXWindow (GLX_NORMAL, gr_window, result_conf);
  105.  
  106.     /* Link the configuration.    */
  107.  
  108.     if (GLXlink(dpy, result_conf) < 0) {
  109.     printf("GLXlink failed\n");
  110.     exit(-1);
  111.     }
  112.  
  113.     /* Set up viewing and color information in the normal 
  114.      * buffer window.
  115.      */
  116.  
  117.     if (GLXwinset(dpy, gr_window) < 0) {
  118.     printf("GLXwinset failed for graphics init\n");
  119.     exit(-1);
  120.     }
  121.     reshapeviewport();
  122.     ortho2(0,1,0,1);
  123.     clear();
  124.  
  125.     /* Here is the X11 event loop.  Each time you get an event,
  126.      * the rectangle in the pop-up planes flips to the other
  127.      * half of the window.
  128.      */
  129.  
  130.     i = 0;
  131.     for (;;) {
  132.  
  133.     XNextEvent(dpy, &event);
  134.  
  135.     /* Draw the normal buffer window.    */
  136.  
  137.     if (GLXwinset(dpy, gr_window) < 0) {
  138.         printf("GLXwinset failed for graphics event\n");
  139.         exit(-1);
  140.     }
  141.  
  142.     if (event.type == MotionNotify) {
  143.         RGBcolor(0,0,0);
  144.         clear();
  145.         RGBcolor(0,128,128);
  146.         bgnline();
  147.         ptv[0] = 0.0; 
  148.         ptv[1] = 1.0; 
  149.         v2f(ptv);
  150.         ptv[0] = event.xmotion.x/256.0; 
  151.         ptv[1] = 1.0 - (event.xmotion.y/256.0);
  152.         v2f(ptv);
  153.         endline();
  154.     }
  155.     }
  156. }
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166. unsigned long GetGLXConfigValue (buffer, mode, sconf)
  167. int buffer;
  168. int mode;
  169. GLXconfig *sconf;
  170.  
  171. {
  172.     int i;
  173.  
  174.     for (i = 0; sconf[i].buffer; i++) {
  175.     if (sconf[i].buffer == buffer &&
  176.         sconf[i].mode   == mode ) {
  177.         return sconf[i].arg;
  178.     }
  179.     }
  180.  
  181.     return 0;
  182. }
  183.  
  184. XVisualInfo * GetGLXVisual (dpy, buffer, sconf)
  185. Display *dpy;
  186. int buffer; 
  187. GLXconfig *sconf;
  188.  
  189. {
  190.     XVisualInfo template;
  191.     int n;
  192.  
  193.     template.visualid = GetGLXConfigValue (buffer, GLX_VISUAL, sconf);
  194.  
  195.     return XGetVisualInfo(dpy, VisualIDMask, &template, &n);
  196.  
  197. }
  198.  
  199. void SetGLXWindow (buffer, w, sconf)
  200. int buffer; 
  201. Window w;
  202. GLXconfig *sconf;
  203.  
  204. {
  205.     int i;
  206.  
  207.     for (i = 0; sconf[i].buffer; i++) {
  208.     if ( (sconf[i].buffer == buffer) &&
  209.          (sconf[i].mode   == GLX_WINDOW)) {
  210.         sconf[i].arg = w;
  211.     }
  212.     }
  213.  
  214. }
  215.